library("tidyverse")
library("firatheme")
library("estimatr")

# load data
load("~/df_replication.RData")

#Table 1
table(df_replication$party_name)
prop.table(table(df_replication$party_name))


# Figure 2
figure2 <- subset(df_replication, !is.na(StartDate))
rects <- data.frame(xstart = seq(0,18720,1440), xend = seq(1440, 20160, 1440))

ggplot() +
  geom_rect(data=rects, aes(ymin=0, ymax=400, xmin=xstart,
                            xmax=xend), col = "white", alpha = 0.1) + scale_fill_manual(values = alpha("red", 0.2)) +
  geom_histogram(data = figure2, aes(x = time_until_click), binwidth = 120, fill = 'gray50') +
  theme_fira() + 
  labs(y="Count", x="Minutes", title="", caption = "Note: N = 1527 \nBin width = 120 minutes \n1 rectangle = 1 day") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), text = element_text(size=8), legend.position= "none", plot.caption = element_text(size = 6, color = "gray40", face = "italic")) 


# main models: click - OLS
#cue
model1 <- lm_robust(click ~ cue, data = df_replication, se_type="HC2")

#issue
model2 <- lm_robust(click ~ issue, data = df_replication, se_type="HC2")

# cue + block
model3 <- lm_robust(click ~ cue + region + party_name, data = df_replication, se_type="HC2")

# issue + block
model4 <- lm_robust(click ~ issue + region + party_name, data = df_replication, se_type="HC2")

# cue + issue + block
model5 <- lm_robust(click ~ cue + issue + region + party_name, data = df_replication, se_type="HC2")

# cue*issue + block
model6 <- lm_robust(click ~ cue*issue + region + party_name, data = df_replication, se_type="HC2")

#cue*incumbent + issue*incumbent + block
model7 <- lm_robust(click ~ cue*incumbent + issue*incumbent + region + party_name, data = df_replication, se_type="HC2")

#cue*elected + issue*elected + block
model8 <- lm_robust(click ~ cue*elected + issue*elected + region + party_name, data = df_replication, se_type="HC2")


# Figure 3 - upper panel (based on model 1 above)
figure3.1 <- tibble(
  Treatment = c("Problem Indicators","Public Opinion", "Party Positions", "Media Reporting"),
  avg = c(round(coef(model1)[1]*100,1), round(coef(model1)[1]*100,1) + round(coef(model1)[2]*100,1),  round(coef(model1)[1]*100,1) + round(coef(model1)[3]*100,1),  round(coef(model1)[1]*100,1) + round(coef(model1)[4]*100,1)), 
  lower = c(round(coef(model1)[1]*100,1) - round(model1[["std.error"]][["(Intercept)"]]*100,4), round(coef(model1)[1]*100,1) + round(coef(model1)[2]*100,1) - round(model1[["std.error"]][["cuepublic_opinion"]]*100,4), round(coef(model1)[1]*100,1) + round(coef(model1)[3]*100,1) - round(model1[["std.error"]][["cuerival_parties"]]*100,4), round(coef(model1)[1]*100,1) + round(coef(model1)[4]*100,1) - round(model1[["std.error"]][["cuethe_media"]]*100,4)),
  upper = c(round(coef(model1)[1]*100,1) + round(model1[["std.error"]][["(Intercept)"]]*100,4), round(coef(model1)[1]*100,1) + round(coef(model1)[2]*100,1) + round(model1[["std.error"]][["cuepublic_opinion"]]*100,4), round(coef(model1)[1]*100,1) + round(coef(model1)[3]*100,1) + round(model1[["std.error"]][["cuerival_parties"]]*100,4), round(coef(model1)[1]*100,1) + round(coef(model1)[4]*100,1) + round(model1[["std.error"]][["cuethe_media"]]*100,4)))

ggplot(figure3.1, aes(x= reorder(Treatment,-avg),y=round(avg,1),fill=Treatment)) +
  geom_bar(stat="identity",position="dodge", color= "black", width=.25)+
  geom_linerange(aes(ymin=lower, ymax=upper), position=position_dodge(.7))+
  geom_text(aes(y=upper, label=format(round(avg, digits=1), nsmall = 1), size = 12), position=position_dodge(width=0.7), vjust=-.75)+
  labs(y="Click Rate (%)", x="", title="", caption = "Note: Error bars represent ± 1 standard error (HC2)")+
  scale_fill_manual(values=c("grey80","gray80", "grey80", "grey80")) + 
  scale_y_continuous(limits = c(0, 35)) + theme_fira() + ggtitle("a") +
  theme(text = element_text(size=8), legend.position= "none", plot.caption = element_text(size = 6, color = "gray40", face = "italic"),
        plot.title = element_text(hjust = 0))

# Figure 3 - lower panel (based on model 5 above)
estimates <- c(0,round(coef(model5)[2]*100,1), round(coef(model5)[3]*100,1), round(coef(model5)[4]*100,1))
lower95 <- c(0,confint(model5)[2,1] * 100, confint(model5)[3,1] * 100, confint(model5)[4,1] * 100) 
upper95 <- c(0,confint(model5)[2,2] * 100, confint(model5)[3,2] * 100, confint(model5)[4,2] * 100) 
lower90 <- c(0,confint(model5, level = 0.90)[2,1] * 100, confint(model5, level = 0.90)[3,1] * 100, confint(model5, level = 0.90)[4,1] * 100) 
upper90 <- c(0,confint(model5, level = 0.90)[2,2] * 100, confint(model5, level = 0.90)[3,2] * 100, confint(model5, level = 0.90)[4,2] * 100) 
labels <- c("Problem Indicators", "Public Opinion", "Party Positions", "Media Reporting")
figure3.2 <- data.frame(estimates, lower95, upper95, lower90, upper90, labels)

ggplot(figure3.2, aes(x=reorder(labels, -estimates), y=estimates, color = labels)) + #color = type 
  geom_point(size=2, position=position_dodge(width=0.3)) +
  geom_linerange(aes(ymin=lower90, ymax=upper90), size=1.5, position=position_dodge(width=0.3)) + 
  geom_linerange(aes(ymin=lower95, ymax=upper95), size=0.5, position=position_dodge(width=0.3)) + 
  theme_fira() + geom_hline(yintercept=0, linetype="dashed", color = "gray50", size=0.5) +
  scale_color_manual(values=c("gray50", "gray50", "gray50", "gray50")) +
  theme(legend.position= "none") + labs(x="", y="Δ Percentage points", title="b", caption = "Note: 'Problem Indicators' is the reference category. Error bars represent 90% and 95% Confidence Intervals") +
  theme(axis.title = element_text(size=6), legend.position= "none", plot.caption = element_text(size = 6, color = "gray40", face = "italic"),
        plot.title = element_text(hjust = 0), strip.text.x = element_blank())


